package com.lindroy.androidplayer.logic.network.datasource

import com.lindroy.androidplayer.logic.model.ApiResponse
import com.lindroy.androidplayer.logic.model.ArticleResp
import com.lindroy.androidplayer.logic.model.PageDataModel
import com.lindroy.androidplayer.logic.model.bean.NavigationIndexGroupItemBean
import com.lindroy.androidplayer.logic.network.ApiService
import com.lindroy.androidplayer.logic.network.RetrofitBuilder
import com.lindroy.androidplayer.utils.kvIsShowTopArticle
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.async
import kotlinx.coroutines.withContext

/**
 * @author Lin
 * @function 网络请求数据源
 */
object NetworkDataSource {

    private val apiService = RetrofitBuilder.create<ApiService>()

    suspend fun login(pwd: String, name: String) = apiService.login(pwd, name)

    suspend fun register(userName: String, password: String, rePassword: String) =
        withContext(Dispatchers.IO) {
            val registerInfo = apiService.register(userName, password, rePassword)
            login(password, userName).takeIf { registerInfo.data != null } ?: registerInfo
        }

    suspend fun logout() = apiService.logout()

    suspend fun getBanner() = apiService.getBanner()

    suspend fun getHomeArticles(pageNo: Int): ApiResponse<PageDataModel<ArticleResp>> =
        withContext(Dispatchers.IO) {
            val articleList = async { apiService.getArticles(pageNo) }
            if (pageNo == 0 && kvIsShowTopArticle) {
                val topArticles = async { apiService.getTopArticles() }
                articleList.await().apply {
                    topArticles.await().data?.also {
                        this.data?.datas?.addAll(0, it)
                    }
                }
            } else {
                articleList.await()
            }
        }

    suspend fun getProjectTypes() = apiService.getProjectTypes()

    suspend fun getProjectList(cid: Int, pageNo: Int, pageSize: Int) =
        apiService.getProjectList(pageNo, cid, pageSize)

    suspend fun getWxArticleChapters() = apiService.getWXChapters()

    suspend fun getWxArticles(id: Int, pageNo: Int, pageSize: Int) =
        apiService.getWxArticles(id, pageNo, pageSize)

    suspend fun getSquareArticles(pageNo: Int, pageSize: Int) =
        apiService.getSquareArticles(pageNo, pageSize)

    suspend fun getKnowledgeSystemChapters() = apiService.getKnowledgeSystemChapters()

    suspend fun getKnowledgeArticles(cid: Int, pageNo: Int, pageSize: Int) =
        apiService.getKnowledgeArticles(pageNo, cid, pageSize)

    suspend fun getQuestionList(pageNo: Int, pageSize: Int) =
        apiService.getQuestionList(pageNo, pageSize)

    suspend fun getNavigationData() = apiService.getNavigationData()

    suspend fun getMyCoinInfo() = apiService.getMyCoinInfo()

    suspend fun getCoinHistory(pageNo: Int) = apiService.getCoinHistory(pageNo)

    suspend fun getCoinRanking(pageNo: Int) = apiService.getCoinRanking(pageNo)

    suspend fun getCollectedArticles(pageNo: Int) = apiService.getCollectedArticles(pageNo)

    suspend fun getCollectedWebsite() = apiService.getCollectedWebsites()

    suspend fun getMyShareList(pageNo: Int, pageSize: Int) =
        apiService.getMyShareList(pageNo, pageSize)

    suspend fun getSearchHotKey() = apiService.getSearchHotKey()

    suspend fun getSearchResult(keyword: String, pageNo: Int, pageSize: Int) =
        apiService.getSearchResult(pageNo, keyword, pageSize)
}